package cn.edu.bjut.explosion;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import cn.edu.bjut.explosion.Http.HtmlService;
import cn.edu.bjut.explosion.JsonGson.Heweather;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    //    private SystemBarTintManager tintManager;
    private NavigationView navigationView;
    private LinearLayout head;
    private ImageView userpicture;

    private String url = "https://free-api.heweather.com/s6/weather/now?location=CN101010100&key=fd19d4d7cd4c494dac928ad6539c7875";
    private TextView cityname;
    private TextView weather;
    private TextView wendu;
    private TextView shidu;
    private TextView fengli;
    private TextView fengxiang;

    private String cityName;
    private String Weather;
    private String Wendu;
    private String Shidu;
    private String Fengli;
    private String Fengxiang;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        drawerLayout = (DrawerLayout) findViewById(R.id.cehua);
        navigationView = (NavigationView) findViewById(R.id.nav);

        cityname = findViewById(R.id.cityname);
        weather = findViewById(R.id.weather);
        wendu = findViewById(R.id.wendu);
        shidu = findViewById(R.id.shidu);
        fengli = findViewById(R.id.fengli);
        fengxiang = findViewById(R.id.fengxiang);

        View headerView = navigationView.getHeaderView(0);//获取头布局
        head = headerView.findViewById(R.id.head);
        userpicture = headerView.findViewById(R.id.userpicture);
        navigationView.setNavigationItemSelectedListener
                (new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //item.setChecked(true);

                switch (item.getItemId()){
                    case R.id.weixin://点击菜单，跳出侧滑菜单
                        Toast.makeText(MainActivity.this,"假装打开了朋友圈",Toast.LENGTH_LONG).show();
                        drawerLayout.closeDrawer(navigationView);
                        break;
                    case R.id.weibo://点击菜单，跳出侧滑菜单
                        Toast.makeText(MainActivity.this,"假装打开了新浪微博",Toast.LENGTH_LONG).show();
                        drawerLayout.closeDrawer(navigationView);
                        break;
                    case R.id.ditu://点击菜单，跳出侧滑菜单
                        Toast.makeText(MainActivity.this,"假装打开了地图",Toast.LENGTH_LONG).show();
                        drawerLayout.closeDrawer(navigationView);
                        break;
                    case R.id.fanyi://点击菜单，跳出侧滑菜单
                        startActivity(new Intent(MainActivity.this,TranslateActivity.class));
                        drawerLayout.closeDrawer(navigationView);
                        break;
                    case R.id.shezhi:
                        startActivity(new Intent(MainActivity.this, SettingActivity.class));
                        drawerLayout.closeDrawer(navigationView);
                        break;
                    case R.id.exit:
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        drawerLayout.closeDrawer(navigationView);
                        finish();
                        break;
                }
//
                return true;
            }
        });


        try {
            tianqi();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private void tianqi() throws InterruptedException {
        Thread tian = new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    String html = HtmlService.getHtml(url);
                    Log.d("Json:",html);
                    Gson gson = new Gson();
                    Heweather bean = gson.fromJson(html,Heweather.class);
                    cityName = bean.getHeWeather6().get(0).getBasic().getLocation();//从处理好的数据中取出所需的数值
                    Weather = bean.getHeWeather6().get(0).getNow().getCond_txt();
                    Wendu = bean.getHeWeather6().get(0).getNow().getTmp();
                    Shidu = bean.getHeWeather6().get(0).getNow().getHum();
                    Fengli = bean.getHeWeather6().get(0).getNow().getWind_sc();
                    Fengxiang = bean.getHeWeather6().get(0).getNow().getWind_dir();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        tian.start();
        tian.join();
        tian.interrupt();
        tian = null;
        cityname.setText(cityName);
        weather.setText(Weather);
        wendu.setText(Wendu+"℃");
        shidu.setText(Shidu+"%");
        fengli.setText(Fengli+"级");
        fengxiang.setText(Fengxiang);


    }

}
